<?php

namespace app\admin\controller;


use think\Request;

class Dish extends Base
{
    //新鲜炒菜列表
    public function all()
    {
        //查询内容
        $dishs = model('Dish')->order('create_time','desc')->paginate(10);
        //模板变量
        $viewData = [
            'dishs' => $dishs
        ];
        //渲染
        $this->assign($viewData);
        return view();
    }

    public function aaa()
    {
        //查询内容
        $dish = model('Dish')->with('detailImg')->order('create_time','desc')->paginate(10);

        /* 去掉文章内容中的html页面标签 */
        foreach ($dish as $key => $value) {
//            $value['dish_content'] = str_replace("&nbsp;","",$value['dish_content']);//去掉空格
            $value['dish_content'] = strip_tags($value['dish_content']);//函数剥去字符串中的 HTML、XML 以及 PHP 的标签,获取纯文本内容
            $dish[$key]['dish_content'] = $value['dish_content'];
        }
//        $this->assign('dish',$dish?$user_article_list:0);
        //模板变量

        return json(['dish' => $dish]);
    }

    //新鲜炒菜添加显示
    public function index()
    {
        return view();
    }

    //新鲜炒菜添加
    public function add(Request $request)
    {
        $data = $request->param();
        $validate = new \app\common\validate\Dish();
        if (!$validate->check($data)){
            return ['code' => 0,'msg' => $validate->getError()];
        }
        //创建模型
        $db = new \app\common\model\Dish();
        //_update接收data
        $result = $db->_update($data);
        if ($result){
            return ['code' => 1,'msg' => '操作成功'];
        }else{
            return ['code' => 0,'msg' => '操作失败，请稍后再试！'];
        }
    }

    //新鲜炒菜编辑
    public function edit(Request $request)
    {
        //接收id
        $id = $request->param('id');
        //实例化模型
        $db = new \app\common\model\Dish();
        //查询轮播id
        $dishInfo = $db->where('id',$id)->find();
        //模板变量
        $viewData = [
            'dishInfo' => $dishInfo
        ];
        //渲染
        $this->assign($viewData);
        return view();
    }

    //新鲜炒菜删除
    public function del()
    {
        $dishInfo = model('Dish')->with('detailImg')->find(input('post.id'));
        foreach ($dishInfo['detailImg'] as $k => $v){/*定位到栏目下的具体文章*/}
        $result = $dishInfo->together('detailImg')->delete();
        if ($result){
            $this->success('删除成功','admin/dish/all');
        }else{
            $this->error('删除失败');
        }
    }
}
